package com.ttc.group2.videochat.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.pagerslidingtabstrip.PagerSlidingTabStrip;
import com.ttc.group2.videochat.R;
import com.ttc.group2.videochat.fragment.GroupFragment;
import com.ttc.group2.videochat.fragment.PeopleFragment;
import com.ttc.group2.videochat.fragment.RecentFragment;
import com.ttc.group2.videochat.utils.DialogUtils;

public class MainActivity extends BaseActivity implements OnPageChangeListener,
		OnClickListener {
	private ViewPager mViewPager;
	private PagerSlidingTabStrip mTabIndicator;
	private VideoChatPagerAdapter mPagerAdapter;
	private LinearLayout mTabs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Custom ActionBar
		customActionBar();
		
		initView();
		List<Fragment> fragments = getFragments();
		mPagerAdapter = new VideoChatPagerAdapter(getSupportFragmentManager(),
				fragments);
		mViewPager.setAdapter(mPagerAdapter);
		mTabIndicator.setViewPager(mViewPager);
//		mViewPager.setOffscreenPageLimit(3);
		mTabIndicator.setOnPageChangeListener(this);
	}

	private void initView() {
		mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
		mTabIndicator = (PagerSlidingTabStrip) findViewById(R.id.tab_indicator);
		mTabs = (LinearLayout) mTabIndicator.getChildAt(0);
	}

	private void customActionBar() {
		mActionbar = getSupportActionBar();
		mActionbar.setDisplayShowHomeEnabled(false);
		mActionbar.setDisplayShowTitleEnabled(false);
		LayoutInflater inflater = LayoutInflater.from(this);
		View customView = inflater
				.inflate(R.layout.custom_main_actionbar, null);
		ImageView ownerAvatar = (ImageView) customView
				.findViewById(R.id.actionbar_main_own);
		ownerAvatar.setOnClickListener(this);
		
		mActionbar.setDisplayShowCustomEnabled(true);
		mActionbar.setCustomView(customView);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.actionbar_main_own) {
			DialogUtils.showToast(this, "Owner Profile", Toast.LENGTH_SHORT);
		}
	}

	private List<Fragment> getFragments() {
		List<Fragment> fragments = new ArrayList<Fragment>();
		fragments.add(RecentFragment.newIntance());
		fragments.add(GroupFragment.newInstance());
		fragments.add(PeopleFragment.newIntance());
		return fragments;
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {

	}

	@Override
	public void onPageSelected(int position) {
		for (int i = 0; i < mTabs.getChildCount(); i++) {
			TextView tv = (TextView) mTabs.getChildAt(i);
			if (i == position) {
				tv.setTextColor(getResources().getColor(
						R.color.videochat_dark_blue));
			} else {
				tv.setTextColor(getResources().getColor(
						R.color.videochat_light_grey));
			}
		}
	}

	public class VideoChatPagerAdapter extends FragmentPagerAdapter {

		private List<Fragment> fragments;

		public VideoChatPagerAdapter(FragmentManager fm, List<Fragment> list) {
			super(fm);
			this.fragments = list;
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			CharSequence title = "";
			switch (position) {
			case 0:
				title = getString(R.string.pager_one);
				break;
			case 1:
				title = getString(R.string.pager_two);
				break;
			case 2:
				title = getString(R.string.pager_three);
				break;
			default:
				break;
			}
			return title;
		}

	}

}
